//********************************************************************************************************
// Product Name: MapWindow.dll Alpha
// Description:  The basic module for MapWindow version 6.0
//********************************************************************************************************
// The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); 
// you may not use this file except in compliance with the License. You may obtain a copy of the License at 
// http://www.mozilla.org/MPL/ 
//
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF 
// ANY KIND, either expressed or implied. See the License for the specificlanguage governing rights and 
// limitations under the License. 
//
// The Original Code is from MapWindow.dll version 6.0
//
// The Initial Developer of this Original Code is Ted Dunsford. Created 6/9/2008 11:25:45 AM
// 
// Contributor(s): (Open source contributors should list themselves and their modifications here). 
//
//********************************************************************************************************

using System.Collections.Generic;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using MapWindow.Geometries;

namespace MapWindow.DirectX
{


    /// <summary>
    /// PositionTexturedVC
    /// </summary>
    public class PositionTexturedVC : VertexCollection
    {
        #region Private Variables


        private bool _isTransparent;
        private int _state;
        private List<Coordinate> _coordinates;
       
      

        #endregion

        #region Constructors

        /// <summary>
        /// Creates a new instance of PositionTexturedVC
        /// </summary>
        public PositionTexturedVC()
        {
            Vertices = new System.Collections.ArrayList();
            _coordinates = new List<Coordinate>();
            Format = CustomVertex.PositionTextured.Format;
            _state = 0;
        }

        /// <summary>
        /// Since we are working with textures, this makes the assumption that
        /// there are four vertices for each texture, marking the corners.
        /// </summary>
        /// <param name="coord"></param>
        public override void AddCoordinate(Coordinate coord)
        {
            // adds the coordinate.  This needs to be overridden 
            
            _state = _state % 4;
            switch (_state)
            {
                case 0: AddCoordinate(coord, 0, 0);
                    break;
                case 1: AddCoordinate(coord, 1, 0);
                    break;
                case 2: AddCoordinate(coord, 0, 1);
                    break;
                case 3: AddCoordinate(coord, 1, 1);
                    break;
            }
            _state++;
        }

        /// <summary>
        /// Adds a new coordinate
        /// </summary>
        /// <param name="coord"></param>
        public override void AddCoordinate(Vector3 coord)
        {
            // adds the coordinate.  This needs to be overridden 
            _state = _state % 4;
            switch (_state)
            {
                case 0: AddCoordinate(coord, 0, 0);
                    break;
                case 1: AddCoordinate(coord, 1, 0);
                    break;
                case 2: AddCoordinate(coord, 0, 1);
                    break;
                case 3: AddCoordinate(coord, 1, 1);
                    break;
            }
            _state++;
        }

        /// <summary>
        /// Automatically assumes that the points are listed as:
        /// TopLeft, TopRight, BottomLeft, BottomRight as far as the texture is concerned
        /// </summary>
        /// <param name="coordinates">The list of coordinates to add</param>
        public PositionTexturedVC(List<Coordinate> coordinates)
        {
            int i = 0;
            foreach (Coordinate coord in coordinates)
            {
                int m = i % 4;
                switch (m)
                {
                    case 0: AddCoordinate(coord, 0, 0);
                        break;
                    case 1: AddCoordinate(coord, 1, 0);
                        break;
                    case 2: AddCoordinate(coord, 0, 1);
                        break;
                    case 3: AddCoordinate(coord, 1, 1);
                        break;
                }
                i++;
            }
        }

        /// <summary>
        /// Adds an individual vertex with the specified color.
        /// </summary>
        /// <param name="coord">The CoordinateF to add</param>
        /// <param name="tU">The horizontal position from 0 to 1 on the texture</param>
        /// <param name="tV">The vertical position from 0 to 1 on the texture</param>
        public virtual void AddCoordinate(Coordinate coord, float tU, float tV)
        {
            _coordinates.Add(coord);    
            CustomVertex.PositionTextured vert = new CustomVertex.PositionTextured(base.DrawBox.ProjToView(coord), tU, tV);
            //vert.Z = 0f;
            Vertices.Add(vert);
        }

        /// <summary>
        /// Adds an individual vertex with the specified color.
        /// </summary>
        /// <param name="coord">The CoordinateF to add</param>
        /// <param name="tU">The horizontal position on the texture from 0(left) to 1(right)</param>
        /// <param name="tV">The vertical position on the texture from 0 (top) to 1 (bottom)</param>
        public virtual void AddCoordinate(Vector3 coord, float tU, float tV)
        {
            CustomVertex.PositionTextured vert = new CustomVertex.PositionTextured(coord, tU, tV);
            Vertices.Add(vert);
        }


        #endregion

        #region Methods

        /// <summary>
        /// Before Drawing methods setup the device for drawing to take place.
        /// </summary>
        /// <param name="device"></param>
        public override void BeforeDrawing(Device device)
        {
            DeviceDX = device;
            
            base.BeforeDrawing(device);
        }

        /// <summary>
        /// The after drawing method resets the parameters that were set in the before drawing
        /// </summary>
        /// <param name="device"></param>
        public override void AfterDrawing(Device device)
        {
           
            base.AfterDrawing(device);
        }

        /// <summary>
        /// Assigns the values to a new vertex buffer
        /// </summary>
        protected override void Initialize()
        {
            if (VertexBuffer != null) VertexBuffer.Dispose();
            // This is specifically set up for the Lines
            //VertexBuffer = new VertexBuffer(DeviceDX, Vertices.Count, Usage.None, CustomVertex.PositionTextured.Format, Pool.Managed);
            VertexBuffer = new VertexBuffer(typeof(CustomVertex.PositionTextured), Vertices.Count, DeviceDX, Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Managed);
            base.Initialize();
        }

        #endregion

        #region Properties


        /// <summary>
        /// gets or sets a value indicating whether the drawing setup should 
        /// also configure the vertices to support transparent textures.
        /// </summary>
        public bool IsTransparent
        {
            get { return _isTransparent; }
            set { _isTransparent = value; }
        }

        /// <summary>
        /// Gets or sets the integer state for this vertex collection
        /// </summary>
        public int State
        {
            get { return _state; }
            set { _state = value; }
        }


        #endregion


        

    }
}
